package stud.aistar.util;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author wangyu
 * @version 1.0
 * @description
 * @createDate 2019/9/27 15:26
 */
@SuppressWarnings("all")
public class SmsUtil {
    private String smsCode;

    public static String code = "";

    public static String getPhonemsg(String mobile) {

        /**
         * 进行正则关系校验
         */
        code = RandomUtils.generateNumberString(6);
        if (mobile != null || mobile != "") {
            String regex = "^((13[0-9])|(14[5|7])|(15([0-3]|[5-9]))|(17[013678])|(18[0,5-9]))\\d{8}$";
            if (mobile.length() != 11){
                System.out.println("手机号应该为11位");
            }else{
                Pattern p = Pattern.compile(regex);
                Matcher m = p.matcher(mobile);
                boolean isMatch = m.matches();
                if (isMatch){

                    /**
                     * 短信验证---阿里大于工具
                     */
                    // 设置超时时间-可自行调整

                    System.setProperty(StaticPeram.defaultConnectTimeout, StaticPeram.Timeout);
                    System.setProperty(StaticPeram.defaultReadTimeout, StaticPeram.Timeout);
                    // 初始化ascClient需要的几个参数
                    final String product = StaticPeram.product;// 短信API产品名称（短信产品名固定，无需修改）
                    final String domain = StaticPeram.domain;// 短信API产品域名（接口地址固定，无需修改）
                    final String accessKeyId = StaticPeram.accessKeyId;
                    final String accessKeySecret = StaticPeram.accessKeySecret;
                    IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
                    try {
                        DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, domain);
                    } catch (ClientException e1) {
                        e1.printStackTrace();
                    }
                    IAcsClient acsClient = new DefaultAcsClient(profile);
                    SendSmsRequest request = new SendSmsRequest();
                    request.setMethod(MethodType.POST);
                    request.setPhoneNumbers(mobile);
                    request.setSignName(StaticPeram.SignName);
                    request.setTemplateCode(StaticPeram.TemplateCode);
                    request.setTemplateParam("{\"code\":\"" + code + "\"}");
                    SendSmsResponse sendSmsResponse;
                    try {
                        sendSmsResponse = acsClient.getAcsResponse(request);
                        if (sendSmsResponse.getCode() != null && sendSmsResponse.getCode().equals("OK")) {
                            // 请求成功
                            System.out.println("获取验证码成功！！！"+sendSmsResponse.getCode());
                            return "true";
                        } else {
                            // 如果验证码出错，会输出错误码告诉你具体原因
                            System.out.println(sendSmsResponse.getCode());
                            System.out.println("获取验证码失败...");
                            return "false";
                        }
                    } catch (ServerException e) {
                        e.printStackTrace();
                        return "由于系统维护，暂时无法注册！！！";
                    } catch (ClientException e) {
                        e.printStackTrace();
                        return "由于系统维护，暂时无法注册！！！";
                    }

                }else {
                    return "手机号不正确";
                }
            }

        }

        return code;
    }

    public String getSmsCode() {
        return smsCode;
    }

    public void setSmsCode(String smsCode) {
        this.smsCode = code;
    }

    public static String getCode() {
        return code;
    }

    public static void setCode(String code) {
        SmsUtil.code = code;
    }
}
